package com.bardpub.controls 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName 
	 * @author LittleBard
	 * @version 1.0
	 * @date 
	 * @descrition 
	 * @others
	 * @history
	 */
	
	public class BCheckBox extends BComponent
	{
		private var __label:String;
		private var __labelField:TextField;
		private var __txtColor:uint;
		private var __nomorlSkin:Sprite;
		private var __selectedSkin:Sprite;
		private var __selected:Boolean;
		private var __hitTest:Sprite;
		private var __value:String;
		
		/**
		 * 创建com.bardpub.controls.BCheckBox类的新实例
		 * @param	parent	父容器
		 * @param	x		x坐标
		 * @param	y		y坐标
		 * @param	label	文本标签
		 */
		public function BCheckBox(parent:DisplayObjectContainer = null, x:Number = 0, y:Number = 0, label:String = "Label")
		{
			__label = label;
			__txtColor = 0;
			__selected = false;
			__value = "";
			
			super(parent, x, y);
		}
		
		/**
		 * 初始化组件
		 */
		override protected function initialize():void 
		{
			super.initialize();
		}
		
		/**
		 * 添加组件的子元素
		 */
		override protected function addChildren():void 
		{
			__labelField = new TextField();
			__labelField.autoSize = "left";
			__labelField.mouseEnabled = __labelField.selectable = false;
			__labelField.defaultTextFormat = new TextFormat("arial");
			addChild(__labelField);
			__labelField.y = -2;
			
			__hitTest = new Sprite();
			addChild(__hitTest);
			__hitTest.graphics.beginFill(0, 0);
			__hitTest.graphics.drawRect(0, 0, 2, 14);
			__hitTest.graphics.endFill();
			__hitTest.buttonMode = __hitTest.useHandCursor = true;
			__hitTest.addEventListener(MouseEvent.CLICK, clickHandler);
			
			try 
			{
				var ref:Class = getClassByName("BCheckBox_nomorl");
				__nomorlSkin = new ref() as Sprite;
				
				ref = getClassByName("BCheckBox_selected");
				__selectedSkin = new ref() as Sprite;
			}
			catch (e:*)
			{
				throw new Error("BCheckBox Create Skin Failed");
			}
		}
		

		/**
		 * 绘制组件UI
		 */
		override public function draw():void 
		{
			__labelField.text = __label;
			__labelField.textColor = __txtColor;
			__labelField.x = __nomorlSkin.width + 3;
			
			var cur:Sprite = __selected ? __nomorlSkin : __selectedSkin;
			var next:Sprite = __selected ? __selectedSkin : __nomorlSkin;
			
			if (cur && cur.parent)
			{
				removeChild(cur);
			}
			if (next && next.parent == null)
			{
				addChild(next);
			}
			setChildIndex(__hitTest, this.numChildren - 1);
			__hitTest.width = __labelField.x + __labelField.width;
			
		}
		
		/***************************************************************/
		/*---                              						    ---*/				
		/*---           getter/setter                               ---*/ 
		/*---                                                       ---*/
		/***************************************************************/
		
		/**
		 * 获取/设置组件的选中状态
		 */
		public function get selected():Boolean
		{
			return __selected;
		}
		[Inspectable(name = "selected", type = "Boolean", defaultValue = false)]
		public function set selected(value:Boolean):void 
		{
			if (value != __selected)
			{
				__selected = value;
				invalidate();
			}
		}
		
		/**
		 * 获取/设置组件的文本标签
		 */
		public function get label():String { return __label; }
		[Inspectable(name = "label", type = "String", defaultValue = "Label")]
		public function set label(value:String):void
		{
			__label = value;
			invalidate();
		}

		/**
		 * 获取/设置组件的文本颜色
		 */
		public function get txtColor():Number 
		{
			return __txtColor;
		}
		[Inspectable(name = "txtColor", type="Number", defalutValue = 0)] 
		public function set txtColor(value:Number):void 
		{
			__txtColor = value;
			invalidate();
		}
		
		/**
		 * 获取/设置组件的值
		 */
		public function get value():String { return __value; }
		[Inspectable(name = "value", type = "String", defaultValue = "")]
		public function set value(v:String):void 
		{
			__value = v;
		}
		
		/***************************************************************/
		/*---                              						    ---*/				
		/*---          event handlers                               ---*/ 
		/*---                                                       ---*/
		/***************************************************************/
		private function clickHandler(e:MouseEvent):void 
		{
			selected = !__selected;
		}
	}

}